﻿// Tribe Framework by Chinajade.
//
// LICENSE:
// This work is licensed under the
//     Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
// also known as CC-BY-NC-SA.  To view a copy of this license, visit
//      http://creativecommons.org/licenses/by-nc-sa/3.0/
// or send a letter to
//      Creative Commons // 171 Second Street, Suite 300 // San Francisco, California, 94105, USA.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Media;

using Styx.Common;


namespace Chinajade.Tribe
{
    [RevisionControlId("$Id: TribeContract.cs 7 2012-12-26 04:50:42Z chinajade247@gmail.com $")]
    public class TribeContract
    {
        // TODO? public delegate bool    ContractPredicateDelegate();
        public delegate string  StringProviderDelegate();


        /// <summary>
        /// <para>This is an efficent poor man's vehicle for reporting contract violations in methods.</para>
        /// <para>If the provided ISCONTRACTOKAY returns true, no action is taken.
        /// If false, a diagnostic message--given by the STRINGPROVIDERDELEGATE--is emitted to the log.</para>
        /// <para>The emitted information can then be used to locate and repair the code misusing the interface. </para>
        /// <para>Notes:<list type="bullet">
        /// <item><description><para> * The interface is built in terms of a StringProviderDelegate, so we don't pay a performance penalty
        /// to build an error message that is not used when ISCONTRACTOKAY is true.</para></description></item>
        /// <item><description><para> * The .NET 4.0 Contract support is insufficient due to the way parts of the project is dynamically compiled
        /// at run time.</para></description></item>
        /// </list></para>
        /// </summary>
        /// <param name="isContractOkay"></param>
        /// <param name="message"></param>
        /// <param name="args"></param>
        ///  30Jun2012-15:58UTC chinajade
        ///  NB: We could provide a second interface to ContractRequires() that is slightly more convenient for static string use.
        ///  But *please* don't!  If helps maintainers to not make mistakes if they see the use of this interface consistently
        ///  throughout the code.
        public static void Requires(bool isContractOkay, StringProviderDelegate stringProviderDelegate = null)
        {
            if (!isContractOkay)
            {
                // TODO: (Future enhancement) Build a string representation of isContractOkay if stringProviderDelegate is null
                Tribe.Log.ContractViolation(new StackTrace(1), stringProviderDelegate());
            }
        }
    }
}
